package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.ZxBx;
import cn.stylefeng.guns.sys.modular.system.entity.ZxBxMingxi;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxBxMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxBxMingxiMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxChengbenMingxiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxBxParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxChengbenResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.ZxBxService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * bty_zx_demo控制器
 *
 * @author liuy
 * @Date 2021-04-07
 */
@Controller
@RequestMapping("/dgdzxBx")
public class DgdZxBxController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdzxBx";
    @Resource
    private ZxBxService zxBxService;
    @Resource
    private ZxBxMingxiMapper zxBxMingxiMapper;
    @Resource
    private ZxChengbenMingxiMapper zxChengbenMingxiMapper;
    @Resource
    private ZxBxMapper zxBxMapper;
    @Resource
    private BtyBxdaService btyBxdaService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben.html";
    }

    @RequestMapping("/index_shenhe")
    public String index_shenhe(Model model, ZxChengbenParam param) {
        return PREFIX + "/shenhe.html";
    }



    /**
     * 跳转到供货商主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("index_ghs")
    public String index_ghs(Model model, ZxChengbenParam param) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("东更道",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("东更道");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
        }

//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_ghs.html";
    }

    /**
     * 装修打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/zx_dakuan")
    public String dakuan(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_dakuan.html";
    }

    /**
     * 装修打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/zx_choudianbaoxiao")
    public String zx_choudianbaoxiao(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_choudianbx.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxChengben_add.html";
    }


    /**
     * 新增或修改
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addGhs")
    public String addGhs(Model model) throws Exception{
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("东更道",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("东更道");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(account);
            bxda.setXm(xingming);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);

        String btyZxChengbenId_str = this.getHttpServletRequest().getParameter("btyZxChengbenId");
        if(!btyZxChengbenId_str.equals("undefined")){
            Long btyZxChengbenId = Long.parseLong(btyZxChengbenId_str);
            zxBxService.chehui(btyZxChengbenId);
        }
        return PREFIX + "/zxChengben_addGhs.html";
    }

    /**
     * 审核页面
     * @author liuy
     * @Date
     */
    @RequestMapping("/shGhs")
    public String shGhs(Model model) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("东更道",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("东更道");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(account);
            bxda.setXm(xingming);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/shGhs.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zxChengben_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/mingxi")
    public String mingxi(Model model) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();

        BtyBxdaResult bxda = btyBxdaService.getByTel("东更道",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("东更道");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(account);
            bxda.setXm(xingming);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/zxChengben_mingxi.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxBxParam zxChengbenParam) {
        zxChengbenParam.setPinpai(pinpai);
        ZxChengbenResult r =  this.zxBxService.add(zxChengbenParam);
        return ResponseData.success(r);
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setPinpai(pinpai);

        this.zxBxService.update(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxBxParam zxChengbenParam) {
        ZxBx vo = this.zxBxMapper.selectById(zxChengbenParam.getBtyZxChengbenId());
        if(vo.getStatus().equals("3")||vo.getStatus().equals("5")){
            if(vo.getCompany()!=null && !vo.getCompany().equals("")){
                Map<String,Object> delMap =new HashMap<String,Object>();
                delMap.put("src_type",vo.getCompany());
                delMap.put("create_user",LoginContextHolder.getContext().getUserId());
                List<ZxChengbenMingxi> list = zxChengbenMingxiMapper.selectByMap(delMap);
                if(list!=null && !list.isEmpty()){
                    for(ZxChengbenMingxi mx:list){
                        if(mx.getStatus().equals("5")||mx.getStatus().equals("3")){
                            ;
                        }else {
                            return new ErrorResponseData("已审批，不能修改,请刷新页面！");
                        }
                    }
                }
            }

        }else {
            return new ErrorResponseData("已审批，不能修改,请刷新页面！");
        }

        //查询有明细则不许删除
        QueryWrapper<ZxBxMingxi> qw = new QueryWrapper<>();
        qw.eq("bty_zx_chengben_id",zxChengbenParam.getBtyZxChengbenId());
        qw.eq("del_flag",0);
        List<ZxBxMingxi> list = zxBxMingxiMapper.selectList(qw);
        if(list.size()>0){
            return new ErrorResponseData("该条目下存在明细，请先删除明细。");
        }
        this.zxBxService.delete(zxChengbenParam);
        return ResponseData.success();
    }
    /**
     * 修改校验算法
     *
     * @author zwf
     * @Date 2021-7-15
     */
    @RequestMapping("/updateValidate")
    @ResponseBody
    public ResponseData updateValidate(ZxBxParam zxChengbenParam) {
        if(zxChengbenParam.getBtyZxChengbenId()!=null){
            ZxBx vo = this.zxBxMapper.selectById(zxChengbenParam.getBtyZxChengbenId());
            if(vo.getStatus().equals("3")||vo.getStatus().equals("5")){
                if(vo.getCompany()!=null && !vo.getCompany().equals("")){
                    Map<String,Object> delMap =new HashMap<String,Object>();
                    delMap.put("src_type",vo.getCompany());
                    delMap.put("create_user",LoginContextHolder.getContext().getUserId());

                    List<ZxChengbenMingxi> list = zxChengbenMingxiMapper.selectByMap(delMap);
                    if(list!=null && !list.isEmpty()){
                        for(ZxChengbenMingxi mx:list){
                            if(mx.getStatus().equals("5")||mx.getStatus().equals("3")){
                                ;
                            }else {
                                return new ErrorResponseData("已审批，不能修改,请刷新页面！");
                            }
                        }
                    }
                }
            }
        }
        return ResponseData.success();
    }


    /**
     * 打款确认
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/dakuan_queren")
    @ResponseBody
    public ResponseData dakuan_queren(ZxChengbenParam zxChengbenParam) {
        this.zxBxService.dakuan_queren(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 取消打款确认
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/dakuan_quxiao")
    @ResponseBody
    public ResponseData dakuan_quxiao(ZxChengbenParam zxChengbenParam) {
        this.zxBxService.dakuan_quxiao(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxChengbenParam zxChengbenParam) {
        ZxBx detail=new ZxBx();
        if(ToolUtil.isNotEmpty(zxChengbenParam.getBtyZxChengbenId())){
           detail = this.zxBxService.getById(zxChengbenParam.getBtyZxChengbenId());
          BigDecimal zongjia=zxBxMingxiMapper.calculateChengbenJine(zxChengbenParam.getBtyZxChengbenId());
          detail.setZongjia(zongjia);
        }

        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxChengbenParam zxChengbenParam) {
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        zxChengbenParam.setPinpai(pinpai);

        return this.zxBxService.findPageBySpec(zxChengbenParam);
    }
    /**
     * 查询列表 审核
     * @author liuy
     * @Date
     */
    @ResponseBody
    @RequestMapping("/listSh")
    public LayuiPageInfo listSh(ZxChengbenParam zxChengbenParam) {
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        zxChengbenParam.setPinpai(pinpai);
        return this.zxBxService.findPageShenhe(zxChengbenParam);
    }

    /**
     * 查询列表 打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list_dk")
    public LayuiPageInfo list_dk(ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setPinpai(pinpai);
        zxChengbenParam.setDaKuan("1");
        return this.zxBxService.findPageBySpec(zxChengbenParam);
    }


    // 查询列表 打款-导出
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response, ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setDaKuan("1");
        zxChengbenParam.setPinpai(pinpai);
        return this.zxBxService.exportExcel(request, response,zxChengbenParam);
    }

    /**
     * 成本信息导入
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public ResponseData ddpx(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxBxService.batchImport(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }

    /**
     * 成本信息导入-报销单导入
     * 导入excel
     */
    @RequestMapping("/importBaoXiaoDan")
    @ResponseBody
    public ResponseData importBaoXiaoDan(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxBxService.batchImportBaoXiaoDan(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }
    /**
     * 投资信息导入
     * 导入excel
     */
    @RequestMapping("/importTz")
    @ResponseBody
    public ResponseData importTz(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxBxService.importTz(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }

    /**
     * 投资信息导入
     * 导入excel
     */
    @RequestMapping("/btnImportTzBatch")
    @ResponseBody
    public ResponseData btnImportTzBatch() throws Exception {
        try {
            ResponseData result = this.zxBxService.btnImportTzBatch();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }


    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-12-09
     */
    @RequestMapping("chengbenAndMingXiIndex")
    public String chengbenAndMingXiIndex(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengbenAndMingXi.html";
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/chengbenAndMingXiList")
    public LayuiPageInfo chengbenAndMingXiList(ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setPinpai(pinpai);
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        return this.zxBxService.findChengbenAndMingXiPageBySpec(zxChengbenParam);
    }

    /**
     * 装修报销-科目纠错-根据明细中其它科目中的文字与科目对应关系，如果关键字对不上将明细中的科目进行修改
     * @return
     */
    @RequestMapping("/kemuCorrection")
    @ResponseBody
    public ResponseData kemuCorrection() {
        return this.zxBxService.kemuCorrection();
    }

    @RequestMapping("/audit")
    @ResponseBody
    public ResponseData audit(ZxBxParam param) {
        param.setPinpai(pinpai);
        return this.zxBxService.audit(param);
    }

}


